﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Text;
using System.Windows;
using System.Windows.Data;
using Microsoft.Practices.Prism.Commands;
using ParaEditor.Common;
using ParaEditor.DAO.ParaDAO;
using ParaEditor.Desktop.Interfaces;
using ParaEditor.Domain.Entities;
using ParaEditor.Infrastructure;

namespace ParaEditor.Desktop.ViewModel
{
    [Export(typeof(IPara3011ViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class Para3011ViewModel : ViewModelBase, IPara3011ViewModel
    {
        private Para3011DAO paramDao;
        private Para3006DAO p3006Dao;
        private Para3007DAO p3007Dao;
        private Para4002DAO p4002Dao;

        private ICollectionView infoVerList;
        public ICollectionView InfoVerList
        {
            get
            {
                return infoVerList;
            }
            set
            {
                this.infoVerList = value;
                this.RaisePropertyChangedEvent("InfoVerList");
            }
        }

        private ICollectionView paraCollectionDetail;
        public ICollectionView ParaCollectionDetail
        {
            get
            {
                return this.paraCollectionDetail;
            }
            set
            {
                this.paraCollectionDetail = value;
                this.RaisePropertyChangedEvent("ParaCollectionDetail");
            }
        }

        private Visibility editGroupBoxVisible;
        public Visibility EditGroupBoxVisible
        {
            get
            {
                return this.editGroupBoxVisible;
            }
            set
            {
                this.editGroupBoxVisible = value;
                this.RaisePropertyChangedEvent("EditGroupBoxVisible");
            }
        }

        private int movAMT;
        public int MovAMT
        {
            get
            {
                return this.movAMT;
            }
            set
            {
                this.movAMT = value;
                this.RaisePropertyChangedEvent("MovAMT");
            }
        }

        private string movDetail;
        public string MovDetail
        {
            get
            {
                return this.movDetail;
            }
            set
            {
                this.movDetail = value;
                this.RaisePropertyChangedEvent("MovDetail");
            }
        }

        //Button
        public DelegateCommand AddParamCommand { get; private set; }
        public DelegateCommand DelParamCommand { get; private set; }
        public DelegateCommand SaveParamCommand { get; private set; }
        public DelegateCommand CanclEditParamCommand { get; private set; }
        public DelegateCommand NewVersionCommand { get; private set; }
        public DelegateCommand CopyCurrentVersionCommand { get; private set; }
        public DelegateCommand ImportCommand { get; private set; }
        public DelegateCommand UpdateCommand { get; private set; }

        public DelegateCommand CreateOffsetCommand { get; private set; }

        private int currentMaxOffset;

        [ImportingConstructor]
        public Para3011ViewModel()
        {
            p3006Dao = new Para3006DAO();
            p3007Dao = new Para3007DAO();
            paramDao = new Para3011DAO();
            p4002Dao = new Para4002DAO();

            currentMaxOffset = 1;
            InfoVerList = CollectionViewSource.GetDefaultView(paramDao.GetAllVersion());
            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
            EditGroupBoxVisible = Visibility.Hidden;

            //Button
            this.AddParamCommand = new DelegateCommand(this.AddParam);
            this.DelParamCommand = new DelegateCommand(this.DelParam);
            this.SaveParamCommand = new DelegateCommand(this.SaveParam);
            this.CanclEditParamCommand = new DelegateCommand(this.CanclEditParam);
            this.NewVersionCommand = new DelegateCommand(this.NewVersion);
            this.CopyCurrentVersionCommand = new DelegateCommand(this.CopyCurrentVersion);
            this.CreateOffsetCommand = new DelegateCommand(this.CreateOffset);
            this.ImportCommand = new DelegateCommand(this.Import);
            this.UpdateCommand = new DelegateCommand(this.Update);
        }

        private void Update()
        {
            ResultValue rv = new ResultValue();
            ObservableCollection<Para3011> NameCnAndPointerMap = paramDao.GetNameCnAndPointerMap();
            //更新station info
            ObservableCollection<Para3006> Para3006Collection = p3006Dao.GetObjectByMaxInfoVer();
            foreach (Para3006 item in Para3006Collection)
            {
                foreach (Para3011 p3011 in NameCnAndPointerMap)
                {
                    if (p3011.movDetail.Equals(item.StationNmCn.Trim()))
                    {
                        rv = p3006Dao.UpdateStationInfoCnNamePointer(item.StationNmCn.Trim(), p3011.para3011Pk.movAmt);
                        if (rv.Code != 0)
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("反更新3006不成功! " + rv.Msg);
                        }
                    }
                }
            }
            //更新line info
            ObservableCollection<Para3007> Para3007Collection = p3007Dao.GetObjectByMaxInfoVer();
            foreach (Para3007 item in Para3007Collection)
            {
                foreach (Para3011 p3011 in NameCnAndPointerMap)
                {
                    if (p3011.movDetail.Equals(item.LineNmCn.Trim()))
                    {
                        rv = p3007Dao.UpdateLineInfoCnNamePointer(item.LineNmCn.Trim(), p3011.para3011Pk.movAmt);
                        if (rv.Code != 0)
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("反更新3007不成功! " + rv.Msg);
                        }
                    }
                }
            }
            //更新ticket type info
            ObservableCollection<Para4002> Para4002Collection = p4002Dao.GetObjectByMaxInfoVer();
            foreach (Para4002 item in Para4002Collection)
            {
                foreach (Para3011 p3011 in NameCnAndPointerMap)
                {
                    if (p3011.movDetail.Equals(item.nameCn.Trim()))
                    {
                        rv = p4002Dao.UpdateTicketTypeInfoCnNamePointer(item.nameCn.Trim(), p3011.para3011Pk.movAmt);
                        if (rv.Code != 0)
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("反更新4002不成功! " + rv.Msg);
                        }
                    }
                }
            }
            Microsoft.Windows.Controls.MessageBox.Show("语言资源指针更新完成!");
        }

        private void Import()
        {
            int infoVer = 0;
            //导入开始
            infoVer = paramDao.GetMaxID() + 1;
            int offset = 0;
            IList<string> listCn = paramDao.GetAllNameCN();
            Para3011 param = null;
            Para3011Pk paramPk = null;
            foreach (string item in listCn)
            {
                paramPk = new Para3011Pk();
                paramPk.infoVer = (short)infoVer;
                paramPk.movAmt = offset;
                param = new Para3011();
                param.para3011Pk = paramPk;
                param.movDetail = item;
                paramDao.CreateObject(param);
                offset += 64;
            }
            //导入完成
            InfoVerList = CollectionViewSource.GetDefaultView(paramDao.GetAllVersion());
            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
            infoVer = (int)InfoVerList.CurrentItem;
            ParaCollectionDetail = CollectionViewSource.GetDefaultView(paramDao.GetObjectByInfoVer(infoVer));

        }

        private void CreateOffset()
        {
            //计算偏移量
            if (InfoVerList.CurrentItem != null)
            {
                if (this.MovDetail != null && !"".Equals(this.MovDetail))
                {
                    //int lengthRes = Encoding.GetEncoding("gb2312").GetByteCount(this.MovDetail);
                    int currentMaxOffset = paramDao.GetMaxOffset((int)InfoVerList.CurrentItem) + 64;
                    this.MovAMT = currentMaxOffset;
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("请填写资源描述！");
                }
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("未选择参数版本号！");
            }
        }

        private void CopyCurrentVersion()
        {
            //查询当前版本
            ObservableCollection<Para3011> currentVersionParam = new ObservableCollection<Para3011>();
            int infoVer = (int)InfoVerList.CurrentItem;
            currentVersionParam = paramDao.GetObjectByInfoVer(infoVer);

            if (currentVersionParam.Count != 0)
            {
                Para3011 param = null;
                Para3011Pk paramPk = null;
                int newVersion = paramDao.GetMaxID() + 1;
                foreach (Para3011 item in currentVersionParam)
                {
                    param = new Para3011();
                    paramPk = new Para3011Pk();
                    //编辑参数
                    paramPk.infoVer = Int16.Parse(newVersion.ToString());
                    paramPk.movAmt = item.para3011Pk.movAmt;
                    param.para3011Pk = paramPk;
                    param.movDetail = item.movDetail;

                    ResultValue result = paramDao.CreateObject(param);
                    if (result.Code != 0)
                    {
                        Microsoft.Windows.Controls.MessageBox.Show("复制过程中出现错误!错误信息：" + result.Msg);
                    }
                }
                //刷新
                ParaCollectionDetail = CollectionViewSource.GetDefaultView(paramDao.GetObjectByInfoVer(newVersion));
                InfoVerList = CollectionViewSource.GetDefaultView(paramDao.GetAllVersion());
                InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("所查询的当前版本没有数据,不可进行复制");
            }
        }
    

        void InfoVerList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (InfoVerList.CurrentItem != null)
            {
                ReflashDataGrid();
            }
        }

        private void ReflashDataGrid()
        {
            int infoVer = (int)InfoVerList.CurrentItem;
            ParaCollectionDetail = CollectionViewSource.GetDefaultView(paramDao.GetObjectByInfoVer(infoVer));
        }
        
        private void AddParam()
        {
            if (InfoVerList.CurrentItem == null)
            {
                Microsoft.Windows.Controls.MessageBox.Show("请添加参数版本！才可进行添加");
            }
            else
            {
                EditGroupBoxVisible = Visibility.Visible;
            }
        }

        private void NewVersion()
        {
            ObservableCollection<int> infoVerCollection = new ObservableCollection<int>();
            infoVerCollection = paramDao.GetAllVersion();
            int newVersion = paramDao.GetMaxID() + 1;
            infoVerCollection.Add(newVersion);
            InfoVerList = CollectionViewSource.GetDefaultView(infoVerCollection);
            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
        }

        public void SaveParam()
        {
            if (Confirm())
            {
                int lengthRes = Encoding.GetEncoding("gb2312").GetByteCount(this.MovDetail);
                if (InfoVerList.CurrentItem != null)
                {
                    currentMaxOffset = paramDao.GetMaxOffset((int)InfoVerList.CurrentItem) + 64;
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("未选择大版本号");
                    return;
                }
                this.MovAMT = currentMaxOffset;

                Para3011 param = new Para3011();
                Para3011Pk paramPk = new Para3011Pk();
                int tmp = (int)InfoVerList.CurrentItem;
                paramPk.infoVer = Int16.Parse(tmp.ToString());
                paramPk.movAmt = this.MovAMT;
                param.para3011Pk = paramPk;
                param.movDetail = this.MovDetail;

                //编辑参数
                ResultValue result = paramDao.CreateObject(param);
                if (result.Code == 0)
                {
                    Microsoft.Windows.Controls.MessageBox.Show("保存成功");
                    ReflashDataGrid();

                    CanclEditParam();
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("保存失败！发生错误:" + result.Msg);
                }
            }

        }

        private void CanclEditParam()
        { }

        private bool Confirm()
        {
            bool flag = true;

            if (this.MovDetail != null && !"".Equals(this.MovDetail))
            {
                flag = true;
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("所填信息不完整");
                flag = false;
            }
            
            return flag;
        }

        private void DelParam()
        {
            try
            {
                if (ParaCollectionDetail.CurrentItem != null)
                {
                    Para3011 param = ParaCollectionDetail.CurrentItem as Para3011;
                    if (Microsoft.Windows.Controls.MessageBox.Show("确认删除所选择的参数！", "3011参数", System.Windows.MessageBoxButton.OKCancel, System.Windows.MessageBoxImage.Question) == System.Windows.MessageBoxResult.OK)
                    {
                        //删除
                        ResultValue result = paramDao.DeleteObject(param,param.para3011Pk.infoVer);
                        if (result.Code == 0)
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("删除成功");
                            //刷新
                            InfoVerList = CollectionViewSource.GetDefaultView(paramDao.GetAllVersion());
                            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
                            ReflashDataGrid();
                        }
                        else
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("删除失败!发生错误：" + result.Msg);
                        }
                    }
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("未选择要删除的参数");
                }
            }
            catch (Exception e)
            {
            }
        }

    }
}
